var GameTexture = require("texture");
var Utils = require("utils");
var EventMgr = require("event_manager");
var Toast = require("toast");

var ALL_LOADING = 71;

var TEST_VERSION = 1;

cc.Class({
    extends: cc.Component,

    properties: {
        gameTexture: null,
        loading: cc.Node,
    },

    onLoad: function () {
        EventMgr.on("socket_login", this.onLoading.bind(this));
        this.isbooleaLoading();
        this.onBack();
    },
    
    onDestroy: function () {
        EventMgr.removeEvent("socket_login");
    },
    
    //是否需要加载资源
    isbooleaLoading: function () {
        var loading_node = cc.find("spr_bg/loading_bg", this.node);
        loading_node.active = false;
        var gamedata_node = cc.find("GameData");
        var gamedata_component = gamedata_node.getComponent("Game_Data");
        if (!gamedata_component.loading) {
            gamedata_component.loading = true;
            var login_node = cc.find("spr_bg/btn_login", this.node);
            login_node.active = false;
            loading_node.active = true;
            
            this.gameTexture = new GameTexture ();
            this.gameTexture.onLoading();
            this.schedule(this.onLoadingRunAction, 0);
        }
    },
    
    //刷新进度条
    onLoadingRunAction: function (dt) {
        var textureNum = this.gameTexture.getLoadingNum();
        cc.log(textureNum);
        this.loading.setContentSize(textureNum/ALL_LOADING * 457,
                                this.loading.height);
        if (textureNum === ALL_LOADING) {
            this.unschedule(this.onLoadingRunAction);
            var login_node = cc.find("spr_bg/btn_login", this.node);
            login_node.active = true;
            var loading_node = cc.find("spr_bg/loading_bg", this.node);
            loading_node.active = false;
        }
    },
    
    //安卓平台下的返回键
    onBack: function () {
        if (Utils.isAndroidos()) {
            //android返回键
            cc.eventManager.addListener({event: cc.EventListener.KEYBOARD,
                onKeyPressed: function(keyCode, event) {
                    if (keyCode === cc.KEY.back) {
                        // back 键
                        cc.director.end();
                    }

                    if (keyCode === cc.KEY.pause) {
                        // home 键
                        cc.director.pause();
                    }
                }
            }, this.node);
        }
    },
    
    //点击微信登录按钮
    onLoginWeiXin: function () {
        if (TEST_VERSION) {
            cc.director.loadScene("lobby_scene", function(err, scene) {
                if (err) {
                    cc.warn("login_component load lobby_scene err");
                }
            });
            return;
        }
        var buf = {"pid":1000,"data": {"openId":"13794457808","nikeName":"testname1000","image":"","sex":1}};
        cc.find("websocket").getComponent("websocket").websocket.send(JSON.stringify(buf));
    },
    
    //登陆回调
    onLoading: function (message) {
        cc.log(message);
        if (message.code == 1) {
            var gamedata_node = cc.find("GameData");
            var gamedata_component = gamedata_node.getComponent("Game_Data");
            {
                gamedata_component.myuserinfo.uid = message.data.uId;
                gamedata_component.myuserinfo.username = message.data.nickName;
                gamedata_component.myuserinfo.diamond = message.data.jewel;
                gamedata_component.myuserinfo.allseason = 475;
                gamedata_component.myuserinfo.winnum = 421;
                gamedata_component.myuserinfo.fanli = 888;
            }
            this.node.runAction(cc.sequence(cc.fadeTo(0.3, 0), cc.callFunc(this.onLoadSceneActionCallback, this)));
        } else {
            Toast.show("登录失败,code:" + message.code, this.node);
        }
    },
    
    //切换场景动画
    onLoadSceneActionCallback: function () {
        cc.director.loadScene("lobby_scene", function(err, scene) {
            if (err) {
                cc.warn("login_component load lobby_scene err");
            }
        });
    },
    
    //点击查看用户使用协议
    onAgreeProtocol: function () {
        
    },
});
